<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="tableText['time']">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="tableText['borrow']">
          <el-select v-model="searchForm.firstLoan">
            <el-option
              v-for="item in tableText['borrowData']"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          :label="tableText['id']"
          min-width="50"
        >
          <template slot-scope="scope">
            {{ setId(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="left"
          prop="rejectDescribe"
          :label="tableText['reason']"
          min-width="200"
        />
        <el-table-column
          prop="inconformityNum"
          :label="tableText['rejected']"
          min-width="100"
        />
        <el-table-column
          prop="inconformityRatio"
          :label="tableText['rejectionRatio']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row['inconformityRatio'] ? (scope.row['inconformityRatio']*100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>
</template>

<script>
import { manpowerRejectStatistics, manpowerRejectStatisticsExport } from '@/api/risk'
import { formatDate } from '@/utils/method'
export default {
  name: 'Refusal',
  data() {
    return {
      loading: true,
      tableData: [],
      allStage: [],
      allFirm: [],
      allGroup: [],
      time: '',
      tableText: {},
      totalNumber: 0,
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('riskReport.rejection')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        createTimeStart: null,
        createTimeEnd: null,
        firstLoan: ''
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = ''
      this.getData()
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    getData() {
      this.loading = true
      manpowerRejectStatistics(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['createTimeStart'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['createTimeEnd'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['createTimeStart'] = null
        data['createTimeEnd'] = null
      }
      return data
    },
    exportList() {
      manpowerRejectStatisticsExport(this.searchData())
    },
    setId(data) {
      const index = this.tableData.findIndex(fruit => fruit === data)
      return (this.searchForm.currentPage - 1) * 10 + index + 1
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
